import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import replay.StateTableFileManager;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.Background;
import com.golden.gamedev.object.GameFont;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.background.ColorBackground;


public class LoserScreen extends GameObject{
	private GameFont myFont;
	private Sprite playButton;
	private Sprite replayButton;
	private Sprite saveButton;
	private String myTitle;
	private Background myBackground;
	private int levelID;
	private StateTableFileManager stfm;
	public LoserScreen(GameEngine engine, int ID) {
		super(engine);
		levelID = ID;
		stfm = new StateTableFileManager();
	}

	@Override
	public void initResources() {
		myTitle = "Bummer, You Lose Try again";
		setFPS(25);
		BufferedImage buttonImage = getImage("images/startbutton.png");
	    myBackground = new ColorBackground(Color.BLUE, 800, 600);
	    replayButton = new Sprite(getImage("images/replayBetter.png"),300,150);
	    replayButton.setBackground(myBackground);
		playButton = new Sprite(buttonImage, 300,300);
		playButton.setBackground(myBackground);
		saveButton = new Sprite(getImage("images/saveButton.png"),400, 300);
		saveButton.setBackground(myBackground);
		myFont = fontManager.getFont(getImages("images/mediumfont.png", 8, 12),
                " !\"#$%&'()*+,-./0123456789:;<=>?" +
                "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" +
                "`abcdefghijklmnopqrstuvwxyz{|}~~");
		
	}

	@Override
	public void render(Graphics2D g) {
		myBackground.render(g);
		playButton.render(g);
		replayButton.render(g);
		saveButton.render(g);
		myFont.drawString(g, myTitle, GameFont.CENTER, 300, 17, 100);
	}

	@Override
	public void update(long elapsedTime) {
		myBackground.update(elapsedTime);
		playButton.update(elapsedTime);
		replayButton.update(elapsedTime);
		saveButton.update(elapsedTime);
		if(click() && checkPosMouse(playButton, true)) //&& (getMouseX() <= myButton.getScreenX()+20 && getMouseX() >= myButton.getScreenX()-20) && 
				//(getMouseY() >= myButton.getScreenY()-20 && getMouseY() <= myButton.getScreenY()+20))
		{
			this.parent.nextGameID = (levelID);
			this.finish();
		}
		if(click() && checkPosMouse(replayButton, true)){
			this.parent.nextGameID = 5;
			((MyBallGame)this.parent).setPrevGameID(levelID);
			this.finish();
		}
		if(click() && checkPosMouse(saveButton, true)){
			stfm.writeToNewSaveFile(((AbstractLevel)((MyBallGame)this.parent).getPrevGame()).getTable(), "default");
		}
	}
}
